package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.CreateGroupRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteGroupRequest;
import com.haohan.jingyuan.customer.dto.req.QueryGroupListRequest;
import com.haohan.jingyuan.customer.dto.req.UpdateGroupRequest;
import com.haohan.jingyuan.customer.dto.resp.GroupInfoResp;
import com.haohan.jingyuan.customer.dto.resp.GroupListResp;
import com.haohan.jingyuan.customer.service.CustomerGroupService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户分组控制器
 *
 * @ClassName CustomerGroupController
 * @Description 客户分组相关接口控制器
 * @Author hexijun
 * @Time 2025/01/19 20:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerGroupController {

    /**
     * 客户分组服务
     */
    private final CustomerGroupService customerGroupService;

    /**
     * 创建分组
     *
     * @param request 创建分组请求
     * @return 创建结果
     */
    @PostMapping("/customer/group/create")
    @NeedLogin
    public GwResponse<GroupInfoResp> createGroup(@Valid @RequestBody GwRequest<CreateGroupRequest> request) {
        log.info("CustomerGroupController.createGroup BEGIN ==> {}", JSONUtil.toJsonStr(request));

        GroupInfoResp groupInfo = customerGroupService.createGroup(request.getRequestData());
        GwResponse<GroupInfoResp> response = GwResponse.success("创建分组成功", groupInfo);

        log.info("CustomerGroupController.createGroup END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 更新分组
     *
     * @param request 更新分组请求
     * @return 更新结果
     */
    @PostMapping("/customer/group/update")
    @NeedLogin
    public GwResponse<GroupInfoResp> updateGroup(@Valid @RequestBody GwRequest<UpdateGroupRequest> request) {
        log.info("CustomerGroupController.updateGroup BEGIN ==> {}", JSONUtil.toJsonStr(request));

        GroupInfoResp groupInfo = customerGroupService.updateGroup(request.getRequestData());
        GwResponse<GroupInfoResp> response = GwResponse.success("更新分组成功", groupInfo);

        log.info("CustomerGroupController.updateGroup END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除分组
     *
     * @param request 删除分组请求
     * @return 删除结果
     */
    @PostMapping("/customer/group/delete")
    @NeedLogin
    public GwResponse<String> deleteGroup(@Valid @RequestBody GwRequest<DeleteGroupRequest> request) {
        log.info("CustomerGroupController.deleteGroup BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerGroupService.deleteGroup(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除分组成功", null);
        } else {
            response = GwResponse.error("删除分组失败");
        }

        log.info("CustomerGroupController.deleteGroup END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询分组列表
     *
     * @param request 查询分组列表请求
     * @return 分组列表
     */
    @PostMapping("/customer/group/list")
    @NeedLogin
    public GwResponse<GroupListResp> queryGroupList(@Valid @RequestBody GwRequest<QueryGroupListRequest> request) {
        log.info("CustomerGroupController.queryGroupList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        GroupListResp groupList = customerGroupService.queryGroupList(request.getRequestData());
        GwResponse<GroupListResp> response = GwResponse.success("查询分组列表成功", groupList);

        log.info("CustomerGroupController.queryGroupList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}
